All files / src/app/api/admin/reviews/[id] route.ts

0% Statements 0/111
100% Branches 0/0
0% Functions 0/1
0% Lines 0/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                                                                                                                               
export const dynamic = "force-dynamic";

import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { prisma } from "@/lib/prisma";
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse,
  ApiErrorResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";

interface RouteParams {
  params: Promise<{ id: string }>;
}

/**
 * GET /api/admin/reviews/[id]
 * Get a single review by ID (admin only)
 */
async function handleGet(_request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const reviewId = parseInt(id);

  if (isNaN(reviewId)) {
    throw ApiError.badRequest("Invalid review ID");
  }

  const review = await prisma.review.findUnique({
    where: { id: reviewId },
    include: {
      product: {
        select: {
          id: true,
          title: true,
          images: {
            select: { url: true, thumbnailUrl: true },
            take: 1 } } },
      user: {
        select: {
          id: true,
          name: true,
          email: true,
          image: true } },
      helpful: {
        select: {
          helpful: true,
          userId: true } } } });

  if (!review) {
    throw ApiError.notFound("Review");
  }

  const helpfulCount = review.helpful.filter((h) => h.helpful).length;
  const notHelpfulCount = review.helpful.filter((h) => !h.helpful).length;

  return successResponse({
    id: review.id,
    rating: review.rating,
    comment: review.comment,
    createdAt: review.createdAt,
    updatedAt: review.updatedAt,
    product: {
      id: review.product.id,
      title: review.product.title,
      image:
        review.product.images[0]?.thumbnailUrl ||
        review.product.images[0]?.url ||
        null },
    user: {
      id: review.user.id,
      name: review.user.name,
      email: review.user.email,
      image: review.user.image },
    helpfulCount,
    notHelpfulCount });
}

/**
 * DELETE /api/admin/reviews/[id]
 * Delete a review (admin only)
 */
async function handleDelete(_request: NextRequest,
  context: RouteContext | undefined): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const reviewId = parseInt(id);

  if (isNaN(reviewId)) {
    throw ApiError.badRequest("Invalid review ID");
  }

  // Check if review exists
  const review = await prisma.review.findUnique({
    where: { id: reviewId } });

  if (!review) {
    throw ApiError.notFound("Review");
  }

  // Delete the review (cascades to ReviewHelpful)
  await prisma.review.delete({
    where: { id: reviewId } });

  return successResponse({ message: "Review deleted successfully" });
}

export const GET = withErrorHandling(withAdmin(handleGet));
export const DELETE = withErrorHandling(withAdmin(handleDelete));